/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.mine.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.modules.mine.entity.Mine;
import com.thinkgem.jeesite.modules.mine.service.MineService;

/**
 * 矿井信息Controller
 * @author developer
 * @version 2018-10-27
 */
@Controller
@RequestMapping(value = "${adminPath}/mine/mine")
public class MineController extends BaseController {

	@Autowired
	private MineService mineService;
	
	@ModelAttribute
	public Mine get(@RequestParam(required=false) String id) {
		Mine entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = mineService.get(id);
		}
		if (entity == null){
			entity = new Mine();
		}
		return entity;
	}
	
	@RequiresPermissions("mine:mine:view")
	@RequestMapping(value = {"list", ""})
	public String list(Mine mine, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Mine> page = mineService.findPage(new Page<Mine>(request, response), mine); 
		model.addAttribute("page", page);
		return "modules/mine/mineList";
	}
	
	@RequiresPermissions("mine:mine:view")
	@RequestMapping(value = {"listJson"})
	@ResponseBody
	public List<Mine> listJson(Mine mine, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Mine> page = mineService.findPage(new Page<Mine>(request, response), mine); 
		return page.getList();
	}
	

	@RequiresPermissions("mine:mine:view")
	@RequestMapping(value = "form")
	public String form(Mine mine, Model model) {
		model.addAttribute("mine", mine);
		return "modules/mine/mineForm";
	}

	@RequiresPermissions("mine:mine:edit")
	@RequestMapping(value = "save")
	public String save(Mine mine, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, mine)){
			return form(mine, model);
		}
		mineService.save(mine);
		addMessage(redirectAttributes, "保存矿井信息成功");
		return "redirect:"+Global.getAdminPath()+"/mine/mine/?repage";
	}
	
	@RequiresPermissions("mine:mine:edit")
	@RequestMapping(value = "delete")
	public String delete(Mine mine, RedirectAttributes redirectAttributes) {
		mineService.delete(mine);
		addMessage(redirectAttributes, "删除矿井信息成功");
		return "redirect:"+Global.getAdminPath()+"/mine/mine/?repage";
	}
	
	/**
	 * 导出用户数据
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("mine:mine:view")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(Mine mine, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "矿井数据"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<Mine> page = mineService.findPage(new Page<Mine>(request, response), mine); 
    		new ExportExcel("矿井信息", Mine.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出用户失败！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/sys/user/list?repage";
    }

}